package com.longxiao.vo;

import java.util.List;

/**
 * Created by houhongwei on 2017/5/9.
 */
public class PageVo<T> {

    int page = 1;

    int maxPage = 1;

    int count = 0;

    int prevPage = 1;

    int nextPage = 1;

    int offset = 0;

    int rows = 20;

    boolean hasPrevPage;

    boolean hasNextPage;

    List<T> data;

    public PageVo(int page, int count) {
        if (page < 1) {
            page = 1;
        }
        this.page = page;
        this.count = count;
        maxPage();
        nextPage();
        prevPage();
        offset();
    }

    @Override
    public String toString() {
        return "Page{" +
                "page=" + page +
                ", maxPage=" + maxPage +
                ", count=" + count +
                ", prevPage=" + prevPage +
                ", nextPage=" + nextPage +
                ", offset=" + offset +
                ", rows=" + rows +
                ", hasPrevPage=" + hasPrevPage +
                ", hasNextPage=" + hasNextPage +
                ", data=" + data +
                '}';
    }

    private int maxPage() {
        maxPage = (int) Math.ceil((double) count / rows);
        return maxPage;
    }

    private void nextPage() {
        if (page >= maxPage) {
            nextPage = page;
        } else {
            nextPage = page + 1;
            hasNextPage = true;
        }
    }

    private void prevPage() {
        if (page <= 1) {
            prevPage = 1;
        } else {
            prevPage = page - 1;
            hasPrevPage = true;
        }
    }


    private void offset() {
        offset = (page - 1) * rows;
        if (offset < 0) {
            offset = 0;
        }
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public int getPage() {
        return page;
    }

    public int getMaxPage() {
        return maxPage;
    }

    public int getCount() {
        return count;
    }

    public List<T> getData() {
        return data;
    }

    public int getPrevPage() {
        return prevPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public int getOffset() {
        return offset;
    }

    public int getRows() {
        return rows;
    }

    public boolean isHasPrevPage() {
        return hasPrevPage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }
}
